package com.net.action.stock;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Commodity;
import com.net.service.CommodityService;
import com.opensymphony.xwork2.ActionSupport;
@Component("priceAction")
@Scope("prototype")
public class PriceAction extends ActionSupport{
	private Commodity commodity;
	private CommodityService commodityService;
	private List<Commodity> price_list;
	private boolean success;
	private int totalProperty;
	private int start;
	private int limit;
	
	public String list(){
		System.out.println(start+":"+limit+"********");
		price_list = commodityService.getCommodities(start/limit, limit, null, null);
		totalProperty = commodityService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String search(){
		//System.out.println(start+":"+limit+"********"+"user name or id:" + commodity.getCommodity_name());
		String str = "";
		if("".equals(commodity.getCommodity_name())){
			str = "c.id="+commodity.getId();
		}else if(commodity.getId()==0){
			str = "c.commodity_name like '%"+commodity.getCommodity_name()+"%'";
		}else {
			str = "c.commodity_name like '%"+commodity.getCommodity_name()+"%'" + " or c.id="+commodity.getId();
		}
		price_list = commodityService.getCommoditiesByCondition(start/limit, limit, str);
		totalProperty = commodityService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String update(){
		System.out.println(commodity.getCurrent_price()+":getCurrent_price");
		float current_price = commodity.getCurrent_price();
		commodity = commodityService.loadCommodityById(commodity.getId());
		commodity.setCurrent_price(current_price);
		commodityService.update(commodity);
		commodity = null;
		success = true;
		return SUCCESS;
	}
	
	public List<Commodity> getPrice_list() {
		return price_list;
	}

	public void setPrice_list(List<Commodity> price_list) {
		this.price_list = price_list;
	}

	public Commodity getCommodity() {
		return commodity;
	}

	public void setCommodity(Commodity commodity) {
		this.commodity = commodity;
	}
	@JSON(serialize=false)
	public CommodityService getCommodityService() {
		return commodityService;
	}
	@Resource(name = "commodityService")
	public void setCommodityService(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getTotalProperty() {
		return totalProperty;
	}

	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	
}
